<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use App\Admin\Models\Order;
use Illuminate\Support\Facades\Input;
use App\Admin\Extensions\OrderDeliver;

class OrderController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('订单管理')
            ->description('订单列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('商家信息编辑')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('评价标签')
            ->description('新增')
            ->body($this->form());
    }
    
//  public function update(Request $request, $id)
//  {
//
//  }
    
//  /**
//   * Handle update.
//   *
//   * @param int $id
//   *
//   * @return \Symfony\Component\HttpFoundation\Response
//   */
//  public function update($id, $data = null)
//  {	
//  	
//      return $this->returnMsg(1, $id);
//  }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order);
//		{
//          "id": 1,
//          "shop_name": "测试店铺2",
//          "user_name": "高耸",
//          "phone": 15881057537,
//          "logo_url": "https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=logo&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=&latest=©right=&cs=3399489119,678504153&os=2135749683,463632745&simid=4106921523,820511717&pn=325&rn=1&di=212520&ln=1725&fr=&fmq=1556444175841_R&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=10e&objurl=http%3A%2F%2Fpic.58pic.com%2F58pic%2F12%2F07%2F11%2F57c58PICJDU.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined",
//          "money": 666,
//          "enable": 1,
//          "create_time": "2019-04-28 17:37:25"
//      }


        $grid->order_id('ID');
        $grid->shop_name('商家名称');
        $grid->user_name('用户姓名');
		$grid->goods_name('商品名称');
		$grid->goods_skus('商品规格')->badge();
        $grid->pay_money('支付金额');
        $grid->order_status('订单状态')->using(['-1' => '退单', '0' => '未发货', '1' => '已发货', '2' => '已收货', '3' => '已评价']);
        $grid->pay_time('下单时间');
        
//      // 不存的字段列
//		$grid->column('full_name')->display(function () {
//		    return $this->first_name.' '.$this->last_name;
//		});

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();
		    
		    $filter->column(1/2, function ($filter) {
		    	
		    	$formartData = array();
		    	$formartData[1] = '金拱门';
		    	$formartData[2] = '开封菜';
		    	
			    $filter->equal('shop_name', '商家名称')->select($formartData);
			    $filter->like('user_name', '用户姓名');
			});
			
			$filter->column(1/2, function ($filter) {
			    $filter->like('order_id', '订单号');
			    $filter->between('pay_time', '下单时间')->datetime();
			});

		});
		
		// 获取当前行数据
		$grid->actions(function ($actions) {
			
//			// 当前行的数据数组
//		    $actions->row;
//		
//		    // 获取当前行主键值
//		    $actions->getKey();
//		    print_r($actions->row->shop_name);exit;

		    // append一个操作
    		//$actions->append('<a class="btn btn-xs btn-default" href=""><i class="fa fa-paper-plane">已发货</i></a>');
    		$actions->append(new OrderDeliver($actions->getKey()));
			
		    $actions->disableDelete();
		    $actions->disableEdit();
//		    $actions->disableView();
		});
		
		// 禁用创建按钮
		$grid->disableCreateButton();

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出数据按钮
		$grid->disableExport();
		
		// 禁用行操作列
		$grid->disableActions();
		
		// 禁用分页条
		//$grid->disablePagination();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$Order = new Order();
        $show = new Show($Order->findOrFail($id));

        $show->order_id('ID');
        $show->shop_name('商家名称');
        $show->user_name('用户姓名');
		$show->goods_name('商品名称');
		$show->goods_skus('商品规格')->badge();;
        $show->pay_money('支付金额');
        $show->order_status('订单状态')->using(['-1' => '退单', '0' => '未发货', '1' => '已发货', '2' => '已收货', '3' => '已评价']);
        $show->pay_time('下单时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(Shop::class, function (Form $form) {
			//form表单显示内容，$form->显示的控件格式【文本框，多选框，文件上传......】（‘数据库中相应的字段’，‘在页面上显示的名称’）->其他方法();
			//$form->text('sort',排序)->default(BrandModel::max('sort') + 1);//default定义默认值为数据表中sort字段的最大值+1
			//$form->image('logo','图片')->uniqueName()->value('1.jpg');//value显示默认图片
			//$form->text('id')->attribute(['readonly' => 'readonly']);
			$form->hidden('id');
			
			$formartData = array();
//			$formartData[0]['shop_id'] = 1;
//			$formartData[0]['shop_name'] = '金拱门';
//			
//			$formartData[1]['shop_id'] = 2;
//			$formartData[1]['shop_name'] = '开封菜';
			
			$formartData['1'] = '金拱门';
			$formartData['2'] = '开封菜';
			
			//$form->select('shop_id', '商家名称')->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);//default定义默认值为数据表中sort字段的最大值+1
			$form->text('user_name', '店主姓名')->rules("required", ['required' => '请填写店主名称']);
			$defaultImg = 'http://images.common.think1st.cn/images/a51e517c7add62e3fa726f5ab3e7d227.jpg';
			$form->image('logo_url', 'logo图片')->uniqueName()->value($defaultImg)->rules("required", ['required' => '请上传商家logo']);
			// 添加属性
			//$form->text('title')->attribute(['data-title' => 'title...']);
//			$form->multipleImage('logo_url', 'logo图')->uniqueName()->removable()->rules("required", ['required' => '请上传商家logo']);

			$form->mobile('phone', '电话')->rules('required|regex:/^[1][0-9]{10}$/', [
        									'required' => '请填写手机号',
        									'regex' => '手机号格式不正确'
        								])->attribute(['style' => 'width: 100%']);
            
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
//			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
			    $footer->disableCreatingCheck();
			
			});
        });
    }
    
    public function goodsDeliver($order_id){
    	
    	return $this->returnMsg(1, 'OK');
    }
}
